$('.fa-chevron-down').click(function(){
    $('#searchCommunity').css('max-height','400px')
})
$('.fa-chevron-up').click(function(){
    $('#searchCommunity').css('max-height','500px')
})
$('#searchData').click(function(){
    $('#searchCommunity').css('max-height','500px')
})
function disableScroll (map) {
    map.disableScrollWheelZoom()
}
function endisableScroll (map) {
    map.enableScrollWheelZoom()
}

var inputData=[];
$('#searchCommunity').css('display','none');
// 查询筛选条件
function initCharts(params){
    $.ajax({
        url:'/map/searchCommunity',
        data:params,
        success:function(data){
            map.clearOverlays();
            var html='';
            var obj=data.result;
            if(obj.length==0){
                $('#searchCommunity').css('display','none');
                return;
            }
            $('#searchCommunity').css('display','block');
            map.centerAndZoom(new BMap.Point(obj[0].lng, obj[0].lat),16);

            $.each(obj,function(i,obj){
                var communityName = obj.communityName;
                var count = obj.count;
                var href = obj.communityId;
                html+=
                    "<li><h5>"+(i+1)+"</h5><div><a href = '"+href+"'>"+communityName+"</a><p>数量"+count+"</p></div></li>"
                var pt = new BMap.Point(obj.lng, obj.lat);
                createMap(pt,communityName,count,href,obj);

            });
            $('#searchCommunity').html(html);
            $('#searchCommunity li').addClass('list-group-item media social-profile clearfix p-b-n');
            $('#searchCommunity li h5').addClass('pull-left text-center p-n m-n');
            $('#searchCommunity li div').addClass('media-body ');
            // 点击小区进入楼栋
            $('#searchCommunity a').click(function(event){
                event.preventDefault();
                params.communityId=$(this).attr("href");
                $.ajax({
                    url:'/map/searchCommunityEstateDate',
                    data:params,
                    success:function(data){
                        map.clearOverlays();
                        var html='';
                        var obj=data.result;
                        map.centerAndZoom(new BMap.Point(obj[0].lng, obj[0].lat),18);
                        var i = 1;
                        obj.forEach(function(data){
                            var estateList = data.estateList;
                            estateList.forEach(function(data){
                                data.forEach(function(data){
                                    html+=     "<li><h5>"+(i++)+"</h5><div><a class='linkMap' id='"+data.id+"'>"+data.sourceAddress+"</a><p>贷款金额："+data.loansAmount+"元</p><p>放款日期："+data.startDate
                                        +" | 贷款期数："+data.loansDuration+"期</p><p>当前评估价："+data.loansEvaluateTotalPrice+"元 | 动态抵押率："+data.cltv+"%</p></div></li>"
                                })
                            })
                        })
                        createMapFloor(obj);
                        $('#searchCommunity').html(html);
                        $('#searchCommunity li').addClass('list-group-item media social-profile clearfix');
                        $('#searchCommunity li h5').addClass('pull-left text-center p-n m-n');
                        $('#searchCommunity li div').addClass('media-body');
                        $('#searchCommunity li p').addClass('m-n');
                        $(".linkMap").click(function(){
                            var href = window.location.origin + "/estate/mapShow/" + this.id;
                            window.open(href);
                        })
                    }
                })
            })

        }
    })
}

// search.onkeyup=function(){
//     $('#searchCommunity').css('display','block');
//     var inputKW=this.value;
//     if(!inputKW){
//         $('#searchCommunity').css('display','none');
//     }
//     inputData={communityName:inputKW};
//     initCharts(inputData)
//     // 地图
//
// }
var EstateDate =  '/map/searchCommunityEstateDate';
function clusterDate (url,zone_number) {
    $.ajax({
        type: "post",
        url: url,
        data:zone_number,
        async: false,
        success:function (data) {
            allAreaResult = data.result;
        }
    })
};
function searchData(){
    $('#searchCommunity').css('max-height','400px')
    var communityName=$("#search").val(),
        city=$('#city').val(),
        district=$('#district').val(),
        risk=$('#risk').val(),
        startDateStarts = $('#startDateStart').val(),
        startDateEnds = $('#startDateEnd').val(),
        buildArea=$('#buildArea').val(),
        loansDuration=$('#loansDuration').val(),
        inputData={
            communityName:communityName,
            city:city,
            district:district,
            risk:risk,
            startDateStarts:startDateStarts,
            startDateEnds:startDateEnds,
            buildArea:buildArea,
            loansDuration:loansDuration
        };
    initCharts(inputData)
}
function createMap(pt,communityName,count,href,obj){
    function ComplexCustomOverlay(point, text,num){
        this._point = point;
        this._text = text;
        this._num = num;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
        this._map = map;
        var div = this._div = document.createElement("div");
        div.style.position = "absolute";
        div.style.backgroundColor = "#26b095";
        div.style.color = "white";
        div.style.height = "60px";
        div.style.width = "60px";
        div.style.textAlign = "center";
        div.style.borderRadius = "50%";
        div.style.whiteSpace = "nowrap";
        div.style.MozUserSelect = "none";
        div.style.fontSize = "12px";
        div.style.cursor = "pointer";
        div.style.zIndex = "-11";
        if(this._num < 5){
            div.style.backgroundColor = "#79ecd6";
        }else if(this._num >= 5 && this._num < 10) {
            div.style.backgroundColor = "#4cdcc0";
        }else if(this._num >= 10 && this._num < 15) {
            div.style.backgroundColor = "#33c3a7";
        }else {
            div.style.backgroundColor = "#26b095";
        }
        var cont = document.createElement("div");
        cont.style.marginTop = "20%";
        var span = this._span = document.createElement("p");
        span.appendChild(document.createTextNode("笔"));
        span.style.margin = "0";
        //var that = this;
        var arrow = this._arrow = document.createElement("p");
        arrow.style.left = "17px";
        arrow.style.margin = "0";
        cont.appendChild(arrow);
        cont.appendChild(span);
        div.appendChild(cont);
        var message = document.createElement("div");
        message.style.width = "160px";
        message.style.height = "50px";
        message.style.background = "#FFFFFF";
        message.style.opacity = ".8";
        message.style.borderTopRightRadius = "25px";
        message.style.borderBottomRightRadius = "25px";
        message.style.position = "absolute";
        message.style.top = "5px";
        message.style.zIndex = "-10";
        message.style.color = "#666666";
        message.style.lineHeight = "50px";
        message.style.display = "none";
        message.style.textAlign = "center";
        message.style.border = "1px solid #e84a01";
        message.style.boxSizing = "border-box";
        message.style.left = "40px";
        message.style.fontSize = "14px";
        message.style.boxShadow = "0 0 6px #092923";
        message.appendChild(document.createTextNode(this._text));
        div.appendChild(message);
        arrow.appendChild(document.createTextNode(this._num));

        div.onmouseenter = function(){
            this.style.backgroundColor = "#e84a01";
            message.style.display = "block";
            this.style.zIndex = "auto";
            message.style.zIndex = "-10";
        };
        div.onmouseleave = function(){
            //this.style.backgroundColor = "#26b095";
            if(this.children[0].children[0].innerText < 5){
                div.style.backgroundColor = "#79ecd6";
            }else if(this.children[0].children[0].innerText >= 5 && this.children[0].children[0].innerText < 10) {
                div.style.backgroundColor = "#4cdcc0";
            }else if(this.children[0].children[0].innerText >= 10 && this.children[0].children[0].innerText < 15) {
                div.style.backgroundColor = "#33c3a7";
            }else {
                div.style.backgroundColor = "#26b095";
            }
            message.style.display = "none";
            this.style.zIndex = "-11";
        };
        div.onclick = function() {
            map.clearOverlays();
            //map.removeOverlay();
            //map.centerAndZoom(new BMap.Point(pt),18);
            var communityId = obj.communityId;
            var communityName=$("#search").val(),
                city=$('#city').val(),
                district=$('#district').val(),
                risk=$('#risk').val(),
                startDateStarts = $('#startDateStart').val(),
                startDateEnds = $('#startDateEnd').val(),
                buildArea=$('#buildArea').val(),
                loansDuration=$('#loansDuration').val(),
                inputData={
                    communityName:communityName,
                    city:city,
                    district:district,
                    risk:risk,
                    startDateStarts:startDateStarts,
                    startDateEnds:startDateEnds,
                    buildArea:buildArea,
                    loansDuration:loansDuration,
                    communityId:communityId
                };
            clusterDate(EstateDate,inputData);
            // console.log(allAreaResult);
            var html = "";
            var i = 1;
            allAreaResult.forEach(function (searchEstate) {
                //console.log(searchEstate);
                var estateList = searchEstate.estateList;
                estateList.forEach(function(data){
                    data.forEach(function(data){
                        // console.log(data);
                        html +=     "<li><h5>"+(i++)+"</h5><div><a class='linkMap' id='"+data.id+"'>"+data.sourceAddress+"</a><p>贷款金额："+data.loansAmount+"元</p><p>放款日期："+data.startDate
                            +" | 贷款期数："+data.loansDuration+"期</p><p>当前评估价："+data.loansEvaluateTotalPrice+"元 | 动态抵押率："+data.cltv+"%</p></div></li>"
                    })
                })
                // console.log(html);
                $('#searchCommunity').html(html);
                $('#searchCommunity li').addClass('list-group-item media social-profile clearfix');
                $('#searchCommunity li h5').addClass('pull-left text-center p-n m-n');
                $('#searchCommunity li div').addClass('media-body');
                $('#searchCommunity li p').addClass('m-n');
                $(".linkMap").click(function(){
                    var href = window.location.origin + "/estate/mapShow/" + this.id;
                    window.open(href);
                })

                //自定义楼栋覆盖物
                function ComplexCustomOverlay(point,num){
                    this._point = point;
                    this._num = num;
                }
                ComplexCustomOverlay.prototype = new BMap.Overlay();
                ComplexCustomOverlay.prototype.initialize = function(map){
                    this._map = map;
                    var div = this._div = document.createElement("div");
                    div.style.position = "absolute";
                    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
                    div.style.backgroundColor = "#26b095";
                    div.style.color = "white";
                    div.style.height = "30px";
                    div.style.lineHeight = "18px";
                    div.style.opacity = '0.9';
                    div.style.textAlign = "center";
                    div.style.borderRadius = "4px";
                    div.style.whiteSpace = "nowrap";
                    div.style.MozUserSelect = "none";
                    div.style.fontSize = "12px";
                    div.style.cursor = "pointer";
                    div.style.padding = "6px";
                    div.appendChild(document.createTextNode(this._num));
                    var sj = this._p = document.createElement("p");
                    sj.style.position = "absolute";
                    sj.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
                    sj.style.width = 0;
                    sj.style.height = 0;
                    sj.style.borderTop = "10px solid #26b095";
                    sj.style.borderLeft = "10px solid transparent";
                    sj.style.borderRight = "10px solid transparent";
                    sj.style.bottom = "-20px";
                    sj.style.left = "25%";
                    div.appendChild(sj);
                    div.onmouseenter = function(){
                        this.style.backgroundColor = "#e84a01";
                        sj.style.borderTop = "10px solid #e84a01";
                    };
                    div.onmouseleave = function(){
                        this.style.backgroundColor = "#26b095";
                        sj.style.borderTop = "10px solid #26b095";
                    };
                    var o = 0;
                    var ul = document.createElement("div");
                    div.onclick = function () {
                        o++;
                        $(this).siblings("div").children("div.hpanel").css("display","none");
                        div.style.zIndex = "auto";
                        $(this).siblings("div").css("zIndex","-10");
                        if(o == 1){
                            ul.className = "hpanel";
                            ul.style.position = "absolute";
                            ul.style.top = "0";
                            ul.style.display = "block";
                            var body = document.createElement("div");
                            body.className = "panel-body list";
                            body.style.overflowY = "scroll";
                            var container = document.createElement("div");
                            container.className = "list-item-container";
                            container.style.maxHeight = "200px";
                            div.appendChild(ul);
                            body.appendChild(container);
                            ul.appendChild(body);
                            var allAreaResult = searchEstate.estateList;

                            allAreaResult.forEach(function(data){
                                data.forEach(function (data){
                                    var item = document.createElement("div");
                                    item.className = "list-item";
                                    item.style.overflowX = "inherit";
                                    var h3 = document.createElement("h3");
                                    h3.className = "no-margins font-extra-bold text-success";
                                    h3.style.fontSize = "12px";
                                    if(data.cltv > 60 && data.cltv <= 80){
                                        h3.style.color = "#FF9800";
                                    }else if(data.cltv > 80 && data.cltv <= 100){
                                        h3.style.color = "#E51C23";
                                    }else if(data.cltv >= 100){
                                        h3.style.color = "#8B1818";
                                    }
                                    h3.appendChild(document.createTextNode(data.sourceAddress));
                                    item.appendChild(h3);
                                    container.appendChild(item);
                                    item.onclick = function () {
                                        var href = window.location.origin + "/estate/mapShow/" + data.id;
                                        window.open(href);
                                    };
                                })
                            });
                            ul.style.left = div.offsetWidth + "px";
                        }else if(ul.style.display == "block"){
                            ul.style.display = "none";
                        }else{
                            ul.style.display = "block"
                        }
                        ul.onmouseenter = function () {
                            disableScroll(map);
                        };
                        ul.onmouseleave = function () {
                            endisableScroll(map);
                        };
                    };
                    map.getPanes().labelPane.appendChild(div);
                    return div;
                };
                ComplexCustomOverlay.prototype.draw = function(){
                    var map = this._map;
                    var pixel = map.pointToOverlayPixel(this._point);
                    this._div.style.left = pixel.x + "px";
                    this._div.style.top  = pixel.y + "px";
                };
                var pt = new BMap.Point(searchEstate.lng,searchEstate.lat);
                var areaResult = searchEstate.estateList[0].length + "笔";
                myCompOverlay = new ComplexCustomOverlay(pt, areaResult);
                map.addOverlay(myCompOverlay);
            });
        };

        map.getPanes().labelPane.appendChild(div);
        return div;
    };
    ComplexCustomOverlay.prototype.draw = function(){
        var map = this._map;
        var pixel = map.pointToOverlayPixel(this._point);
        this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
        this._div.style.top  = pixel.y - 30 + "px";
    };
    var myCompOverlay = new ComplexCustomOverlay(pt,communityName, count);
    map.addOverlay(myCompOverlay);
}
/*function createMapFloor(data){
    var pt = new BMap.Point(data.lng, data.lat);
    console.log(data)
    var areaResult = data.result.length + "套";
    function ComplexCustomOverlay(point,num){
        this._point = point;
        this._num = num;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
        this._map = map;
        var div = this._div = document.createElement("div");
        div.style.position = "absolute";
        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        div.style.backgroundColor = "#26b095";
        div.style.color = "white";
        div.style.height = "30px";
        div.style.lineHeight = "18px";
        div.style.opacity = '0.9';
        div.style.textAlign = "center";
        div.style.borderRadius = "4px";
        div.style.whiteSpace = "nowrap";
        div.style.MozUserSelect = "none";
        div.style.fontSize = "12px";
        div.style.cursor = "pointer";
        div.style.padding = "6px";
        div.appendChild(document.createTextNode(this._num));
        var sj = this._p = document.createElement("p");
        sj.style.position = "absolute";
        sj.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        sj.style.width = 0;
        sj.style.height = 0;
        sj.style.borderTop = "10px solid #26b095";
        sj.style.borderLeft = "10px solid transparent";
        sj.style.borderRight = "10px solid transparent";
        sj.style.bottom = "-20px";
        sj.style.left = "25%";
        div.appendChild(sj);
        var ul = document.createElement("div");
        ul.className = "hpanel";
        ul.style.position = "absolute";
        ul.style.top = "0";
        ul.style.display = "none";

        var body = document.createElement("div");
        body.className = "panel-body list";
        body.style.overflowY = "scroll";
        var container = document.createElement("div");
        container.className = "list-item-container";
        container.style.height = "200px";
        div.appendChild(ul);
        body.appendChild(container);
        ul.appendChild(body);

        div.onmouseenter = function(){
            this.style.backgroundColor = "#e84a01";
            sj.style.borderTop = "10px solid #e84a01";
        };
        div.onmouseleave = function(){
            this.style.backgroundColor = "#26b095";
            sj.style.borderTop = "10px solid #26b095";
        };
        div.onclick = function () {
            ul.style.left = div.offsetWidth + "px";
            if(ul.style.display == "none"){
                ul.style.display = "block";
            }else {
                ul.style.display = "none"
            }
        };
        ul.onmouseenter = function () {
            disableScroll(map);

        };
        ul.onmouseleave = function () {
            endisableScroll(map);
        };
            data.result.forEach(function(data){
                var item = document.createElement("div");
                item.className = "list-item";
                item.style.overflowX = "inherit";
                var h3 = document.createElement("h3");
                h3.className = "no-margins font-extra-bold text-success";
                h3.style.fontSize = "12px";
                h3.appendChild(document.createTextNode(data.sourceAddress));
                item.appendChild(h3);
                container.appendChild(item);
                item.onclick = function () {
                    var href = window.location.origin + "/estate/mapShow/" + data.id;
                    window.open(href);
                };

            });
            // $("#message").toggle(500);
            var view = document.getElementById("view");
            /!*view.onclick = function () {
                window.location.pathname = "estate/show/" + searchEstate.id;
            }*!/

        map.getPanes().labelPane.appendChild(div);
        return div;
    };
    ComplexCustomOverlay.prototype.draw = function(){
        var map = this._map;
        var pixel = map.pointToOverlayPixel(this._point);
        this._div.style.left = pixel.x + "px";
        this._div.style.top  = pixel.y - 30 + "px";
    };
    var myCompOverlay = new ComplexCustomOverlay(pt,areaResult);
    map.addOverlay(myCompOverlay);


}*/
function createMapFloor(data){
    data.forEach(function (searchEstate) {
        //自定义楼栋覆盖物
        function ComplexCustomOverlay(point,num){
            this._point = point;
            this._num = num;
        }
        ComplexCustomOverlay.prototype = new BMap.Overlay();
        ComplexCustomOverlay.prototype.initialize = function(map){
            this._map = map;
            var div = this._div = document.createElement("div");
            div.style.position = "absolute";
            div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
            div.style.backgroundColor = "#26b095";
            div.style.color = "white";
            div.style.height = "30px";
            div.style.lineHeight = "18px";
            div.style.opacity = '0.9';
            div.style.textAlign = "center";
            div.style.borderRadius = "4px";
            div.style.whiteSpace = "nowrap";
            div.style.MozUserSelect = "none";
            div.style.fontSize = "12px";
            div.style.cursor = "pointer";
            div.style.padding = "6px";
            div.appendChild(document.createTextNode(this._num));
            var sj = this._p = document.createElement("p");
            sj.style.position = "absolute";
            sj.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
            sj.style.width = 0;
            sj.style.height = 0;
            sj.style.borderTop = "10px solid #26b095";
            sj.style.borderLeft = "10px solid transparent";
            sj.style.borderRight = "10px solid transparent";
            sj.style.bottom = "-20px";
            sj.style.left = "25%";
            div.appendChild(sj);
            div.onmouseenter = function(){
                this.style.backgroundColor = "#e84a01";
                sj.style.borderTop = "10px solid #e84a01";
            };
            div.onmouseleave = function(){
                this.style.backgroundColor = "#26b095";
                sj.style.borderTop = "10px solid #26b095";
            };
            var o = 0;
            var ul = document.createElement("div");
            div.onclick = function () {
                o++;
                $(this).siblings("div").children("div.hpanel").css("display","none");
                div.style.zIndex = "auto";
                $(this).siblings("div").css("zIndex","-10");
                if(o == 1){
                    ul.className = "hpanel";
                    ul.style.position = "absolute";
                    ul.style.top = "0";
                    ul.style.display = "block";
                    var body = document.createElement("div");
                    body.className = "panel-body list";
                    body.style.overflowY = "scroll";
                    var container = document.createElement("div");
                    container.className = "list-item-container";
                    //container.style.height = "200px";
                    div.appendChild(ul);
                    body.appendChild(container);
                    ul.appendChild(body);
                    ul.appendChild(foldDiv);
                    var allAreaResult = searchEstate.estateList;

                    allAreaResult.forEach(function(data){
                        data.forEach(function (data){
                            var item = document.createElement("div");
                            item.className = "list-item";
                            item.style.overflowX = "inherit";
                            var h3 = document.createElement("h3");
                            h3.className = "no-margins font-extra-bold text-success";
                            h3.style.fontSize = "12px";
                            if(data.cltv > 60 && data.cltv <= 80){
                                h3.style.color = "#FF9800";
                            }else if(data.cltv > 80 && data.cltv <= 100){
                                h3.style.color = "#E51C23";
                            }else if(data.cltv >= 100){
                                h3.style.color = "#8B1818";
                            }
                            h3.appendChild(document.createTextNode(data.sourceAddress));
                            item.appendChild(h3);
                            container.appendChild(item);
                            item.onclick = function () {
                                var href = window.location.origin + "/estate/mapShow/" + data.id;
                                window.open(href);
                            };
                        })
                    });
                    ul.style.left = div.offsetWidth + "px";
                }else if(ul.style.display == "block"){
                    ul.style.display = "none";
                }else{
                    ul.style.display = "block"
                }
                ul.onmouseenter = function () {
                    disableScroll(map);
                };
                ul.onmouseleave = function () {
                    endisableScroll(map);
                };
            };
            map.getPanes().labelPane.appendChild(div);
            return div;
        };
        ComplexCustomOverlay.prototype.draw = function(){
            var map = this._map;
            var pixel = map.pointToOverlayPixel(this._point);
            this._div.style.left = pixel.x + "px";
            this._div.style.top  = pixel.y + "px";
        };
        var pt = new BMap.Point(searchEstate.lng,searchEstate.lat);
        var areaResult = searchEstate.estateList[0].length + "笔";
        myCompOverlay = new ComplexCustomOverlay(pt, areaResult);
        map.addOverlay(myCompOverlay);
    });
}


